((win) => {

    class AES {

        constructor(key, iv) {
            this._key = key;
            this._iv = iv;
        }

        get key() {
            return this._key;
        }

        get iv() {
            return this._iv;
        }

        // 加密
        encrypt(data) {
            let that = this;
            data = CryptoJS.enc.Utf8.parse(data);
            let key = CryptoJS.enc.Utf8.parse(that.key.substr(0, 16));
            let iv = CryptoJS.enc.Utf8.parse(that.iv.substr(0, 16));
            let encrypted = CryptoJS.AES.encrypt(data, key, {
                iv: iv,
                mode: CryptoJS.mode.CBC,
                padding: CryptoJS.pad.Pkcs7
            });
            // 加密结果base64加密
            return btoa(encrypted.toString());
        }

        // 解密
        decrypt(data) {
            let that = this;
            let key = CryptoJS.enc.Utf8.parse(that.key.substr(0, 16));
            let iv = CryptoJS.enc.Utf8.parse(that.iv.substr(0, 16));
            // 加密数据base64解密
            let decrypted = CryptoJS.AES.decrypt(atob(data), key, {
                iv: iv,
                mode: CryptoJS.mode.CBC,
                padding: CryptoJS.pad.Pkcs7
            });
            return decrypted.toString(CryptoJS.enc.Utf8);
        }

    }

    win.AES = AES;

})(window);
